package com.block.datapush.common.factory;

import cn.hutool.core.util.ObjectUtil;
import com.block.common.util.ApplicationContextUtil;
import com.block.datapush.common.enums.FilterIndexEnum;
import com.block.datapush.common.filter.IFilterHandlerService;
import com.block.datapush.common.filter.impl.*;
import com.block.datapush.common.model.entity.FilterEntity;
import com.block.datapush.common.model.param.FilterHandlerParam;
import com.block.datapush.common.model.result.FilterHandlerResult;
import com.block.datapush.form.item.HappyPushHandleService;
import com.block.datapush.form.service.IFormPushHandle;
import com.block.db.entity.MemberExtInfo;

import java.util.HashMap;
import java.util.Map;

public class FilterFactory {

    private static Map<Integer, FilterEntity> serviceMap = new HashMap<>();
    static {
        serviceMap.put(FilterIndexEnum.ITEM_Start.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_Start, FilterCityHandlerService.class , FilterIndexEnum.ITEM_1));
        serviceMap.put(FilterIndexEnum.ITEM_1.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_1 , FilterOrFilterHandlerService.class,  FilterIndexEnum.ITEM_2));
        serviceMap.put(FilterIndexEnum.ITEM_2.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_2 , FilterAgeHandlerService.class,  FilterIndexEnum.ITEM_3));
        serviceMap.put(FilterIndexEnum.ITEM_3.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_3 , FilterLoanAmountHandlerService.class,  FilterIndexEnum.ITEM_4));
        serviceMap.put(FilterIndexEnum.ITEM_4.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_4 , FilterZhiMaHandlerService.class,  FilterIndexEnum.ITEM_5));
        serviceMap.put(FilterIndexEnum.ITEM_5.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_5 , FilterBaiTiaoHandlerService.class,  FilterIndexEnum.ITEM_6));
        serviceMap.put(FilterIndexEnum.ITEM_6.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_6 , FilterHuaBeiHandlerService.class,  FilterIndexEnum.ITEM_7));
        serviceMap.put(FilterIndexEnum.ITEM_7.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_7 , FilterXueLiHandlerService.class,  FilterIndexEnum.ITEM_8));
        serviceMap.put(FilterIndexEnum.ITEM_8.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_8 , FilterHouseHandlerService.class,  FilterIndexEnum.ITEM_9));
        serviceMap.put(FilterIndexEnum.ITEM_9.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_9 , FilterCarHandlerService.class,  FilterIndexEnum.ITEM_10));
        serviceMap.put(FilterIndexEnum.ITEM_10.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_10 , FilterGjjHandlerService.class,  FilterIndexEnum.ITEM_11));
        serviceMap.put(FilterIndexEnum.ITEM_11.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_11 , FilterSheBaoHandlerService.class,  FilterIndexEnum.ITEM_12));
        serviceMap.put(FilterIndexEnum.ITEM_12.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_12 , FilterBaoDanHandlerService.class,  FilterIndexEnum.ITEM_13));
        serviceMap.put(FilterIndexEnum.ITEM_13.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_13 , FilterEnterpriseOwnerHandlerService.class,  FilterIndexEnum.ITEM_14));
        serviceMap.put(FilterIndexEnum.ITEM_14.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_14 , FilterYuQiHandlerService.class,  FilterIndexEnum.ITEM_15));
        serviceMap.put(FilterIndexEnum.ITEM_15.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_15 , FilterIdCardHandlerService.class,  FilterIndexEnum.ITEM_16));
        serviceMap.put(FilterIndexEnum.ITEM_16.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_16 , FilterInChannelArrayHandlerService.class,  FilterIndexEnum.ITEM_17));
        serviceMap.put(FilterIndexEnum.ITEM_17.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_17 , FilterOutChannelArrayHandlerService.class,  FilterIndexEnum.ITEM_18));
        serviceMap.put(FilterIndexEnum.ITEM_18.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_18 , FilterPushNumberHandlerService.class,  FilterIndexEnum.ITEM_41));

        // 推送任务条件过滤
        /**************分界线******************/
        // 推送状态判断
//        serviceMap.put(FilterIndexEnum.ITEM_40.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_40 , FilterPriceAmountHandlerService.class,  FilterIndexEnum.ITEM_41));
        serviceMap.put(FilterIndexEnum.ITEM_41.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_41 , FilterWeekHandlerService.class,  FilterIndexEnum.ITEM_42));
        serviceMap.put(FilterIndexEnum.ITEM_42.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_42 , FilterStatusHandlerService.class,  FilterIndexEnum.ITEM_43));
        serviceMap.put(FilterIndexEnum.ITEM_43.getIndex(), new FilterEntity(FilterIndexEnum.ITEM_43 , FilterTimeHandlerService.class,  FilterIndexEnum.ITEM_End));

    }

    public static FilterHandlerResult execFilterChainService(FilterHandlerParam handlerParam, MemberExtInfo memberExtInfo){

        FilterEntity filterEntity = serviceMap.get(FilterIndexEnum.ITEM_Start.getIndex());

        FilterHandlerResult handlerResult = null;
        while (true){
            IFilterHandlerService handlerService = ApplicationContextUtil.getBean(filterEntity.getAClass());
            handlerResult = handlerService.handlerFilterLogic(handlerParam, memberExtInfo);
            if (!handlerResult.isSuccess()){
                // 验证没有通过，返回
                break;
            }

            filterEntity = serviceMap.get(filterEntity.getNext().getIndex());
            if (ObjectUtil.isEmpty(filterEntity)){
                // 链结束，，直接返回
                break;
            }
        }

        return handlerResult;
    }


}
